home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / comm / tcp / rxsocket.lha / rxsocket / examples / ntblockecho.rexx < prev    next >
OS/2 REXX Batch file  |  2000-11-28  |  3KB  |  117 lines

  1. /*
  2.     Name:        ntblockecho.rexx
  3.  
  4.     Version:     2.0
  5.  
  6.     Author:         alfie
  7.  
  8.     Date:        28.04.98
  9.  
  10.     Description: shows how to make a non blocking TCP connection;
  11.                  the stopping event is a msg on port "PORT";
  12.  
  13.     Usage:          rx ntblockecho <host>
  14.  
  15.     Note:         it is a simple echo client in itself.
  16.                  Be sure to have echo/tcp enabled in the service
  17.                  and inetd database, before testing it on localhost.
  18.                  Anyway, 'cause of a sort of delay is required to
  19.                  test the stopping-event, try this macro on a very
  20.                  slow non-local host with echo/tcp service.
  21. */
  22.  
  23. l="rexxsupport.library";if ~show("L",l) then;if ~addlib(l,0,-30) then do;say "can't find" l;exit;end
  24. l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then do;say "can't find" l;exit;end
  25. l="rxsocket.library";if ~show("L",l) then;if ~addlib(l,0,-30) then do;say "can't find" l;exit;end
  26.  
  27. prg = ProgramName("NOEXT")
  28.  
  29. if ~RMH_ReadArgs("HOST/A") then do
  30.     call PrintFault(IoErr(),prg)
  31.     exit
  32. end
  33.  
  34. addr = resolve(parm.0.value)
  35. if addr=="-1" then call err "no host <"parm.0.value">"
  36.  
  37. /*set the remote addr*/
  38. sin.addrFamily = "INET"
  39. sin.addrAddr   = addr
  40.  
  41. /*try to find the echo service via GetServByName()*/
  42. if GetServByName("SE","echo","tcp") then sin.addrPort = SE.servPort
  43. else sin.addrPort = 7
  44.  
  45. /*open a port, a message on which will stop us*/
  46. if ~OpenPort("PORT") then call err "can't open my port"
  47.  
  48. /*get the port signal mask */
  49. portSig = PortSignal("PORT")
  50.  
  51. /*allocate a signal we want to be notified on for a socket events*/
  52. sigBit=AllocSignal()
  53. if sigBit==-1 then call err "can't allocate signal bit"
  54. sig=2**sigBit
  55.  
  56. /*create the socket*/
  57. sock = socket("INET","STREAM","IP")
  58. if sock<0 then call err "can't create socket ("errno()")"
  59.  
  60. /*the socket must NOT block*/
  61. call IOCtlSocket(sock,"FIONBIO",1)
  62.  
  63. /*notif us on connect*/
  64. call SetSockOpt(sock,"SOCKET","EVENTMASK","CONNECT")
  65.  
  66. /*notify us via that signal we allocated*/
  67. SET.SIGEVENTMASK = sig
  68. call SetSocketBase("SET")
  69.  
  70. /*connect the echo service on the host*/
  71. res = connect(sock,"SIN")
  72. err=errno()
  73. if (res<0) & (err~=35) & (err~=36) then call err "connect error ("errno()")"
  74.  
  75. /*we even set a timeout of 10 seconds*/
  76. timeout = 10
  77. signal = or(portSig,sig)
  78. SEL.WRITE.0 = sock
  79. con = 0
  80. time = time("R")
  81. do while ~con & (time("E")<timeout)
  82.  
  83.     res = WaitSelect("SEL",timeout,0,signal)
  84.  
  85.     if and(SEL.SIGNALS,portSig)~=0 then do
  86.         pkt = GetPkt("PORT")
  87.         if pkt~=null() then do
  88.             m = GetArg(pkt)
  89.             call reply(pkt)
  90.             say "message on PORT:" m
  91.         end
  92.     end
  93.  
  94.     if res==1 then con = 1;
  95.  
  96. end
  97.  
  98. if ~con then call err "timeout"
  99.  
  100. say "--- connected ---"
  101.  
  102. call IOCtlSocket(sock,"FIONBIO",0)
  103.  
  104. request = "echo service test"
  105. res = send(sock,request)
  106. if res~=length(request) then call err "send errore ("errno()")"
  107.  
  108. len = recv(sock,"BUFF",256)
  109. if len<0 then call err "recv error ("errno()")"
  110. say buff
  111. exit
  112.  
  113. err: procedure expose prg
  114. parse arg msg
  115.     say prg":" msg
  116.     exit
  117.